﻿using Bandit.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ETC.HallICElan2In1.Helpers
{
    public class HallSensorHelper : DeviceBase
    {
        public HallSensorHelper(string portName) : base(portName)
        {

        }

        #region 实例
        private static HallSensorHelper _Instance;
        /// <summary>
        /// 实例
        /// </summary>
        public static HallSensorHelper Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new HallSensorHelper(ConfigHelper.GetVauleToString("ComPort"));
                }
                return _Instance;
            }
            set
            {
                _Instance = value;
            }
        }
        #endregion

        public static void Reset()
        {
            if (_Instance != null)
            {
                Instance.IsAutoReopen = false;
                Instance.BanditSerialPort.Close();
                Instance.BanditSerialPort = null;
                Instance = null;
            }
        }

        //protected override void BanditSerialPort_OnReceived(object sender, OnReceivedArgs e)
        //{

        //    _ResponseData = e.NormalData + this.BanditSerialPort.EndFlag;

        //    LogHelper.Info(string.Format("SeriaPort Recv -> {0}", _ResponseData));

        //    base.Set();

        //}

        public override void Send(string cmd, bool isThread = false, int delayMS = 200)
        {
            this._ResponseData = string.Empty;

            LogHelper.Info(string.Format("SeriaPort Send -> {0}", cmd));

            if (isThread)
            {
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(delayMS);

                    this.BanditSerialPort.Send(cmd, DataTypeBySerialPort.Text, true);

                }).LogExceptions("Send - " + cmd);
            }
            else
            {
                this.BanditSerialPort.Send(cmd, DataTypeBySerialPort.Text, true);

            }
        }

    }
}
